package com.mwy.quartz;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class QuartzManager {
    //注入调度器
    @Autowired
    private Scheduler scheduler;

    //新增定时任务
    public void addJob(String jobName,String jobGroupName,String triggerName,String triggerGroupName,Class jobClass,String cron){
        try {
        //获取任务详情
        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity(jobName,jobGroupName)
                .build();
        //创建触发器
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName,triggerGroupName)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
        //利用调度器将任务和触发器绑定起来
            scheduler.scheduleJob(jobDetail,trigger);
            //启动调度器
            if(!scheduler.isShutdown()){
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //修改定时任务
    public void changeJob(String jobName,String jobGroupName,String triggerName,String triggerGroupName,Class jobClass,String cron){
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);
            CronTrigger trigger = (CronTrigger)scheduler.getTrigger(triggerKey);
            if(trigger == null){
                return;
            }
            String oldCron = trigger.getCronExpression();
            if(!oldCron.equals(cron)){
                CronTrigger triggerNew = TriggerBuilder.newTrigger()
                        .withIdentity(triggerName, triggerGroupName)
                        .startNow()
                        .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                        .build();
                scheduler.rescheduleJob(triggerKey,trigger);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //删除定时任务
    public void removeJob(String jobName,String jobGroupName,String triggerName,String triggerGroupName){
        try {
            //通过触发器名字和组名获取触发器实例
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
            scheduler.pauseTrigger(triggerKey);    //停止触发器
            scheduler.unscheduleJob(triggerKey);   //移除触发器
            //通过任务名称和组名获得任务实例
            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
            scheduler.resumeJob(jobKey);    //删除任务
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }


    //启动所有定时任务
    public void startAllJob(){
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //关闭所有定时任务
    public void endAllJob(){
        try {
            if(scheduler.isShutdown()){
                scheduler.shutdown();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }



}
